package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.LostAndFound.dao.OrderListForSearchLoserDao;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.utils.DataSourceUtils;

public class OrderListForSearchLoserService {

	OrderListForSearchLoserDao dao = new OrderListForSearchLoserDao();

	public int getTotalToOrderForEasyUI(String cid) {
		int total = 0;
		try {
			total=dao.getgetTotalToOrderForEasyUI(cid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}

	public List<Map<String, Object>> getOrderListForEasyUI(int pageNumber, int pageSize, String cid) {
		List<Map<String, Object>> mapList = null;
		int startIndex = (pageNumber-1)*pageSize;
		
		try {
			mapList = dao.getOrderListForEasyUI(startIndex,pageSize,cid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return mapList;
	}

	public boolean handleCancelClaim(String aid) {
		boolean success = false;
		try {
			dao.handleCancelClaim(aid);
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}

	// 使用事务来控制success的同步变化。写入foundArticle表，更改article表的foundflag字段值为0
	public boolean handleConfirmClaim(FoundArticle foundArticle) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleConfirmClaim(foundArticle);
			dao.handleChangeArticleFoundFlag(foundArticle);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
}
